using System;
using System.Collections.Generic;
using System.Linq;
using BearUtil;
using BearUtil.Hidden;
using UnityEngine;

public class FormatDataUtil
{
    public const string episode_id = "episode_id";
    public const string dialogue_id = "dialogue_id";
    public const string remark = "remark";
    
    public static void SetData(ref FormatData data, BearNode node)
    {
        // 兼容旧数据
        var bn = node as DialogNode;
        if (bn != null)
            setDefaultData((DialogNodeFormat)data, bn);
        else
        {
            var dataType = node.GetType();
            var infos = dataType.GetFields();
            for (int i = 0; i < infos.Length; i++)
            {
                try
                {
                    // startwith "_" 表示不进入数据分析过程，自定义属性的辅助属性需求
                    var info = infos[i];
                    var typeName = info.FieldType.Name.ToLower();
                    var fieldName = info.Name.ToLower();
                    if (!info.DeclaringType.Name.Equals(dataType.Name) ||
                        fieldName.Equals("audioclip") || 
                        fieldName.Equals("index") ||
                        fieldName.Equals("selfGraph") ||
                        info.Name.StartsWith("_"))
                        continue;

                    switch (typeName)
                    {
                        case "vector2":
                            var propX = info.Name + "_x";
                            var propY = info.Name + "_y";
                            Vector2 pos = (Vector2) info.GetValue(node);
                            data.GetType().GetField(propX).SetValue(data, pos.x);
                            data.GetType().GetField(propY).SetValue(data, pos.y);
                            break;
                        default:
                            data.GetType().GetField(info.Name).SetValue(data, info.GetValue(node));
                            break;
                    }
                }
                catch (Exception e)
                {
                    Debug.LogError(string.Format("[StoryDataFormat] error info: {0} : {1} ", infos[i].Name,
                        data.GetType().Name));
                    Debug.LogError(e);
                }
            }
        }
    }

    private static void setDefaultData(DialogNodeFormat data, DialogNode node)
    {
        // try
        // {
        //     // data.episode_id = Convert.ToInt32((node.graph as StoryGraph).basic.id);
        //     
        //     // data.dialogue_id = node.basic.id.Trim().Equals("") ? 0 : Convert.ToInt32(node.basic.id);
        // }
        // catch (Exception e)
        // {
        //     Debug.LogError("StoryGraph 配置出错, 请检查 Dialog Node 数据");
        //     Debug.LogError(e);
        // }
        //
        data.trigger_param = node.property.triggerParams?.ToArray();
        data.map_id = node.property.map_id;
        
        List<int> events = new List<int>();
        foreach (var propertyTriggerEvent in node.property.triggerEvents)
            events.Add((int)propertyTriggerEvent);
        
        data.trigger_event = events.ToArray();
        
        data.camera_pos_x = node.property.camera_pos.x;
        data.camera_pos_y = node.property.camera_pos.y;
        
        data.pc_pos_x = node.property.pc_pos.x;
        data.pc_pos_y = node.property.pc_pos.y;
        
        data.pc_direction = (int)node.property.pc_direction;
        data.next_map_id = node.property.auto_trans_map_id;
        
        // 临时新增，目前 remark 功能没有用
        data.remark = node.basic.name;
    }
    
    public static Type GetType(string name)
    {
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Reverse())
        {
            var tt = assembly.GetType(name);
            if (tt != null)
            {
                return tt;
            }
        }

        return null;
    }
}
